exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
  constructor() {
    super();
    this.operationModel = this.db['sys_operation'];
  }

  /**
   * 获取权限标识数据
   * @param {*} data {page,pageSize}
   */
  getOperation(data) {
    var _this = this;

    return _asyncToGenerator(function* () {
      data.start = (parseInt(data.page) - 1) * parseInt(data.pageSize);
      const Op = _this.db.sequelize.Op;
      const where = {};

      if (data.name) {
        where.name = {
          [Op.like]: '%' + data.name + '%'
        };
      }
      if (data.code) {
        where.code = {
          [Op.like]: '%' + data.code + '%'

        };
      }
      const condition = {
        where: {},
        limit: parseInt(data.pageSize),
        offset: parseInt(data.start)
      };
      try {
        return yield _this.operationModel.findAndCountAll(condition);
      } catch (error) {
        return _this.exceptionService.handleError(error);
      }
    })();
  }

  /**
   * 新增/修改 权限标识
   * @param {*} data
   */
  upDataOperation(data) {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      const Op = _this2.db.Sequelize.Op;

      try {
        const olderOperation = yield _this2.operationModel.findOne({
          where: {
            [Op.or]: {
              name: data.name,
              code: data.code
            }
          }
        });
        if (data.id) {
          // 编辑
          if (olderOperation && olderOperation.id !== data.id) {
            return { error: '数据不匹配，请重试' };
          }
          return yield _this2.operationModel.update(data, {
            where: {
              id: data.id
            }
          });
        } else {
          // 新增
          if (olderOperation) {
            return { error: '标识名称或标识码重复，请重试' };
          }
          return yield _this2.operationModel.create(data);
        }
      } catch (error) {
        return _this2.exceptionService.handleError(error);
      }
    })();
  }

  /**
   * 删除权限标识
   * @param {*} data
   */
  delOperation(data) {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      const where = {
        id: data.id
      };
      try {
        return yield _this3.operationModel.destroy({ where: where });
      } catch (error) {
        return _this3.exceptionService.handleError(error);
      }
    })();
  }

  getOperationAll() {
    var _this4 = this;

    return _asyncToGenerator(function* () {
      try {
        return _this4.operationModel.findAll();
      } catch (error) {
        return _this4.exceptionService.handleError(error);
      }
    })();
  }
};